package com.sanjishu.enterlab.electricity.sale.controller;

import com.sanjishu.enterlab.electricity.sale.domain.City;
import com.sanjishu.enterlab.electricity.sale.domain.CompanyBak;
import com.sanjishu.enterlab.electricity.sale.domain.CompanyInfo;
import com.sanjishu.enterlab.electricity.sale.domain.DetailInfo;
import com.sanjishu.enterlab.electricity.sale.domain.responseModel.ResponseCompany;
import com.sanjishu.enterlab.electricity.sale.service.*;
import com.sanjishu.enterlab.electricity.sale.shiro.ShiroAdmin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("companyInfo")
public class CompanyInfoController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private FileService fileService;
    @Autowired
    private BLDistinguishService blDistinguishService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private CityService cityService;
    @Autowired
    private DetailInfoService detailInfoService;
    @Autowired
    private CompanyBakService companyBakService;

    @RequestMapping(value = "new", method = RequestMethod.GET)
    public String newCompany(Model model) {
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<CompanyInfo> companyInfoList = companyInfoService.findByUserId(loginUser.getId());
        model.addAttribute("companyInfoList", companyInfoList);
        return "company_info/edit";
    }

    @RequestMapping(value = "new/pro", method = RequestMethod.GET)
    public String newCompanyT(Model model) {
        model.addAttribute("from", "1");
        return newCompany(model);
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public String editCompany(@PathVariable Integer id, Model model) {
        CompanyInfo companyInfo = companyInfoService.findById(id);
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<CompanyInfo> companyInfoList = companyInfoService.findOtherCompany(loginUser.getId(), id);
        DetailInfo detailInfo = companyInfoService.findByCompanyId(id);
        model.addAttribute("companyInfoList", companyInfoList);
        model.addAttribute("companyInfo", companyInfo);
        model.addAttribute("detailInfo", detailInfo);
        return "company_info/edit";
    }

    @RequestMapping(value = "edit/{id}/pro", method = RequestMethod.GET)
    public String editCompanyT(@PathVariable Integer id, Model model) {
        model.addAttribute("from", "1");
        return editCompany(id, model);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Response saveCompany(Integer companyId, String companyName, String creditCode, String operName, String address, String elecFor,
                              String contractName, String contractPhone, String wxNum, String capital,
                              String establishDate, String types, String validPeriod, String imgUrl) {
        CompanyInfo companyInfo = null;
        if (companyId != null) {
            companyInfo = companyInfoService.updateCompany(companyId, companyName, creditCode, operName, address, elecFor,
                    contractName, contractPhone, wxNum, capital,
                    establishDate, types, validPeriod, imgUrl, null, null, null);
        } else {
            Subject subject = SecurityUtils.getSubject();
            ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
            companyInfo = companyInfoService.createCompany(companyName, creditCode, operName, address, elecFor,
                    contractName, contractPhone, wxNum, capital,
                    establishDate, types, validPeriod, imgUrl, loginUser.getId(), null, null, null, null);
        }
        return Response.getSuccess(companyInfo);
    }

    @RequestMapping(value = "newDetail/{id}", method = RequestMethod.GET)
    public String newDetail(@PathVariable Integer id, Model model) {
        CompanyInfo companyInfo = companyInfoService.findById(id);
        if (companyInfo != null && companyInfo.getCityId() != null) {
            City city = cityService.getById(companyInfo.getCityId());
            CompanyBak companyBak = companyBakService.getOneByName(companyInfo.getName());
            model.addAttribute("city", city);
            model.addAttribute("companyBak", companyBak);
        }
        List<City> cityList = cityService.getCityList();
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<CompanyInfo> companyInfoList = companyInfoService.findOtherCompany(loginUser.getId(), id);
        model.addAttribute("companyInfoList", companyInfoList);
        model.addAttribute("cityList", cityList);
        model.addAttribute("companyInfo", companyInfo);
        return "detail_info/edit";
    }

    @RequestMapping(value = "newDetail/{id}/pro", method = RequestMethod.GET)
    public String newDetialT(@PathVariable Integer id, Model model) {
        model.addAttribute("from", "1");
        return newDetail(id, model);
    }

    @RequestMapping(value = "editDetail/{id}", method = RequestMethod.GET)
    public String editDetail(@PathVariable Integer id, Model model) {
        DetailInfo detailInfo = companyInfoService.findByDetailId(id);
        CompanyInfo companyInfo = companyInfoService.findById(detailInfo.getCompanyInfoId());
        Subject subject = SecurityUtils.getSubject();
        ShiroAdmin loginUser = (ShiroAdmin) subject.getPrincipal();
        List<CompanyInfo> companyInfoList = companyInfoService.findOtherCompany(loginUser.getId(), companyInfo.getId());
        model.addAttribute("companyInfoList", companyInfoList);
        if (companyInfo != null && companyInfo.getCityId() != null) {
            City city = cityService.getById(companyInfo.getCityId());
            CompanyBak companyBak = companyBakService.getOneByName(companyInfo.getName());
            model.addAttribute("city", city);
            model.addAttribute("companyBak", companyBak);
        }
        List<City> cityList = cityService.getCityList();
        model.addAttribute("cityList", cityList);
        model.addAttribute("companyInfo", companyInfo);
        model.addAttribute("detailInfo", detailInfo);
        return "detail_info/edit";
    }

    @RequestMapping(value = "editDetail/{id}/pro", method = RequestMethod.GET)
    public String editDetailT(@PathVariable Integer id, Model model) {
        model.addAttribute("from", "1");
        return editDetail(id, model);
    }

    @RequestMapping(value = "detailUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Response saveDetail(Integer id, Integer companyId, String voltageStep, String accountArr,
                               String transformerCapacity, String elecConsumption,
                               Integer cityId, String detailCategory, String bankName, String bankAccountName,
                               String bankAccount, String contactPhone, String fax) {
        DetailInfo detailInfo = null;
        if (id != null) {
            detailInfo = detailInfoService.updateDetailInfo(id, companyId, voltageStep, accountArr,
                    transformerCapacity, elecConsumption, cityId, detailCategory, bankName,
                    bankAccountName, bankAccount, contactPhone, fax);
        } else {
            detailInfo = detailInfoService.createDetailInfo(companyId, voltageStep, accountArr,
                    transformerCapacity, elecConsumption, cityId, detailCategory, bankName,
                    bankAccountName, bankAccount, contactPhone, fax);
        }
        return Response.getSuccess(detailInfo);
    }

    @RequestMapping(value="uploadImg",method= RequestMethod.POST)
    @ResponseBody
    public Response uploadImg(MultipartFile file) throws Exception{
        log.info("上传图片");
        ResponseCompany company = blDistinguishService.getMessage(file);
        String imgUrl = fileService.uploadImg1(file);
        company.setImgUrl(imgUrl);
        return Response.getSuccess(company);
    }


}
